import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';

import { PeopleService } from '../../app/people.service';

import { App } from 'ionic-angular';
import { LoginPage } from '../login/login';

import { People } from '../../app/people';
import { modifyUserURL } from '../../app/someConst';
import {Http, Headers, RequestOptions} from '@angular/http';

import { PeopleListPage } from '../people-list/people-list';

/**
 * Generated class for the PeopleDetailPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */

@Component({
  selector: 'page-people-detail',
  templateUrl: 'people-detail.html',
})
export class PeopleDetailPage {

  userName: string;
  password: string;
  user: People;

  constructor(public navCtrl: NavController, 
              public navParams: NavParams,
              public peopleService: PeopleService,
              public app: App,
              public http: Http) {
              
  

  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad PeopleDetailPage');
  }

  loginOutAction(){
    // this.app.getRootNav.set
    this.app.getRootNav().setRoot(LoginPage);
  }

  updateUserName(){

    if(!this.userName){
      alert('请添加用户名');
      return;
    }


    let body = new FormData();
    body.append('user_id', this.peopleService.people.user_id);
    body.append('user_name', this.userName);
    this.http.post(modifyUserURL, body).map(res => res.json()).subscribe(
      data => {
        console.log(data);
        if(data.code == 200){
              this.user = data.result as People;
              this.peopleService.people = this.user;
              // this.app.getRootNav().setRoot(PeopleDetailPage); 
            } else {
              // let message = data.message;
              alert(data.message);
              // return;
            }
      },
      err => {

      }
    )
  }

  userListAction(){
    this.navCtrl.push(PeopleListPage);
  }










}
